<template>
  <div>
    <div>
      <el-form inline>
        <el-form-item label="名称">
          <el-input v-model="formModel.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="formModel.createdAt"
            type="date"
            placeholder="请选择时间"
          />
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="getList">查询</el-button>
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <el-table :data="list.data" style="width: 100%">
      <el-table-column prop="name" label="名称" width="180" />
      <el-table-column label="图片" width="180">
        <template v-slot:default="{ row }">
          <el-image
            :preview-src-list="[`http://${row.fileUrl}`]"
            class="w-full h-150px"
            preview-teleported
            :src="`http://${row.fileUrl}`"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="创建时间">
        <template v-slot:default="{ row }">
          {{ dayjs(row.createdAt).format("YYYY-MM-DD HH:mm") }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template v-slot:default="{ row }">
          <div>
            <el-button type="primary" @click="toDetail(row.id)">查看</el-button>
            <el-button type="danger" @click="deleteRow(row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script setup lang="ts">
import { TrackDelete, TrackListGet } from "@/api/track";
import dayjs from "dayjs";

const formModel = ref({
  name: "",
  createdAt: "",
  pageSize: 10,
  pageIndex: 1,
});

const router = useRouter();
function add() {
  router.push("/track/create");
}
const list = ref<any>({
  data: [],
});

function toDetail(id: number) {
  router.push({
    path: "/track/edit",
    query: {
      id,
    },
  });
}
async function deleteRow(id: number) {
  try {
    await TrackDelete(id);
    ElMessage.success("操作成功");
  } catch (error) {
    ElMessage.error("操作失败");
  }
}

async function getList() {
  list.value = await TrackListGet(formModel.value);
}
onMounted(() => {
  getList();
});
</script>
